<?php
// ====================================================
// filename:wl101_add.php
// summary: 用户管理画面_添加用户
// ====================================================
require_once ("../init.php");
if (! isset($_SESSION['wl_uid'])) {
    echo json_encode(array(
        'success' => false,
        'msg' => "登陆信息已过期，请重新登陆！"
    ));
    exit();
}

php_handle();
function php_handle()
{
    $uid = $_SESSION['wl_uid'];
    $cid = $_SESSION['wl_cid'];
    $user_login = isset($_POST['user_login']) ? $_POST['user_login'] : "";
    $user_name = isset($_POST['user_name']) ? $_POST['user_name'] : "";
    $user_tel = isset($_POST['user_tel']) ? $_POST['user_tel'] : "";
    $user_remark = isset($_POST['user_remark']) ? $_POST['user_remark'] : "";
    
    if (empty($user_login)) {
        echo json_encode(array(
            'success' => false,
            'msg' => "登录账号不能为空，请确认！"
        ));
        return;
    }
    
    if (empty($user_name)) {
        echo json_encode(array(
            'success' => false,
            'msg' => "用户名不能为空，请确认！"
        ));
        return;
    }
    
    if (empty($user_tel)) {
        echo json_encode(array(
            'success' => false,
            'msg' => "手机号码不能为空，请确认！"
        ));
        return;
    }
    
    $wlu02 = new wlu02tbx();
    $u02_1res = $wlu02->sel_wlu02tbx_by_user_login($user_login);
    if (count($u02_1res) > 0) {
        echo json_encode(array(
            'success' => false,
            'msg' => "输入的登录账号已存在，请确认！"
        ));
        return;
    }
    
    $u02_2res = $wlu02->sel_wlu02tbx_by_user_tel($user_tel);
    if (count($u02_2res) > 0) {
        echo json_encode(array(
            'success' => false,
            'msg' => "输入的手机号码已存在，请确认！"
        ));
        return;
    }
    
    $u02res = $wlu02->sel_wlu02tbx_for_max_user_id($cid);
    if (count($u02res) > 0) {
        $user_id = $u02res[0]['max_id'] + 1;
    } else {
        $user_id = $cid . '001';
    }
    
    $u02add = array();
    $u02add["user_id"] = $user_id;
    $u02add["company_id"] = $cid;
    $u02add["user_login"] = $user_login;
    $u02add["user_tel"] = $user_tel;
    $u02add["user_name"] = $user_name;
    $u02add["user_pw"] = md5 ('000000');
    $u02add["user_remark"] = $user_remark;
    
    $u02rec = $wlu02->insert($uid, $u02add);
    if ($u02rec == 0) {
        echo json_encode(array(
            'success' => false,
            'msg' => "数据提交失败！"
        ));
        return;
    }
    
    echo json_encode(array(
        'success' => true,
        'data' => $user_id
    ));
    return;
}

?>
